package org.apache.beam.examples.Models.enums;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import org.apache.beam.examples.Models.QualityRuleResultModel;
import org.apache.beam.examples.Models.QualityRuleColData;

import java.time.LocalDateTime;

public enum QualityType {


    OutNounItem("超出了字典约束") {
        @Override
        public QualityRuleResultModel execute(QualityRuleColData qualityRuleColData) {
            log.debug("正在检查是否有字典超出了约束，检查字段为：{}", qualityRuleColData);
            QualityRuleResultModel model = new QualityRuleResultModel();
            long count = qualityRuleColData.getDetailModel().getNoun().getChildren().stream().filter(noun -> noun.getCode().equals(qualityRuleColData.getDataMap().get(qualityRuleColData.getDetailModel().getColumnCode().toUpperCase()))).count();
            if (count == 0) {
                model.setResult(Boolean.FALSE);
                log.debug("存在字典超出的问题！【{}】", qualityRuleColData);
            }else {
                model.setResult(Boolean.TRUE);
            }
            model.setQualityRuleRowData(qualityRuleColData);
            model.setId(DateUtil.format(LocalDateTime.now(), "yyyyMMddHHmmssSSS"));

            return model;
        }
    },
    hasNull("存在空值") {
        @Override
        public QualityRuleResultModel execute(QualityRuleColData qualityRuleColData) {
            log.debug("正在检查是否有存在空值，检查字段为：{}", qualityRuleColData);
            QualityRuleResultModel model = new QualityRuleResultModel();
            model.setResult(!StrUtil.isEmptyIfStr(qualityRuleColData.getDataMap().get(qualityRuleColData.getDetailModel().getColumnCode().toUpperCase())));
            model.setQualityRuleRowData(qualityRuleColData);
            model.setId(DateUtil.format(LocalDateTime.now(), "yyyyMMddHHmmssSSSS"));
            return model;
        }
    };

    static final Log log = LogFactory.get();
    private final String value;

    QualityType(String value) {
        this.value = value;
    }


    public String getValue() {
        return value;
    }


    /**
     * 根据枚举定义的过程
     *
     * @param qualityRuleColData 配置的数据
     * @return 检查结果数据
     */
    public abstract QualityRuleResultModel execute(QualityRuleColData qualityRuleColData);

}
